package com.cardbaobao.cardbabyclient.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;

/**
 * Created by Jiangzx on 15:28.
 */

public class TitleBar extends LinearLayout {
    public TextView tvLeft;
    public TextView tvTitle;
    public TextView tvRight;
    public View divider;
    private RelativeLayout ll_right;
    private ImageView iv_right_pic;
    private ImageView iv_right_pic1;
    private RelativeLayout ll_left;
    private ImageView iv_left_pic;

    public TitleBar(Context context) {
        this(context, null, 0);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.wigdet_title_bar, this);
        tvLeft = (TextView) contentView.findViewById(R.id.tv_title_left);
        tvTitle = (TextView) contentView.findViewById(R.id.tv_title_title);
        tvRight = (TextView) contentView.findViewById(R.id.tv_title_right);
        divider = contentView.findViewById(R.id.view_title_bar_divider);

        ll_right = (RelativeLayout) contentView.findViewById(R.id.rl_right);
        iv_right_pic = (ImageView) contentView.findViewById(R.id.iv_right_pic);
        iv_right_pic1 = (ImageView) contentView.findViewById(R.id.iv_right_pic1);

        ll_left = (RelativeLayout) contentView.findViewById(R.id.rl_left);
        iv_left_pic = (ImageView) contentView.findViewById(R.id.iv_left_pic);

        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TitleBar);
            int count = a.getIndexCount();
            int attr;
            for (int i = 0; i < count; i++) {
                attr = a.getIndex(i);
                switch (attr) {
                    case R.styleable.TitleBar_titleBarBg:
                        //titleBar背景
                        Drawable bg = a.getDrawable(attr);
                        if (bg != null) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                                contentView.setBackground(bg);
                            } else {
                                contentView.setBackgroundDrawable(bg);
                            }
                        }
                        break;
                    case R.styleable.TitleBar_tvLeftVisibility:
                        tvLeft.setVisibility(a.getBoolean(attr, false) ? VISIBLE : GONE);
                        break;
                    case R.styleable.TitleBar_tvLeftBg:
                        Drawable btLeftBg = a.getDrawable(attr);
                        if (btLeftBg != null) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                                tvLeft.setBackground(btLeftBg);
                            } else {
                                tvLeft.setBackgroundDrawable(btLeftBg);
                            }
                        }
                        break;
                    case R.styleable.TitleBar_tvLeftText:
                        String textLeft = a.getString(attr);
                        if (!TextUtils.isEmpty(textLeft)) {
                            tvLeft.setText(textLeft);
                        }
                        break;
                    case R.styleable.TitleBar_tvLeftTextColor:
                        int textLeftColor = a.getColor(attr, 0);
                        if (textLeftColor != 0) {
                            tvLeft.setTextColor(textLeftColor);
                        }
                        break;
                    case R.styleable.TitleBar_tvLeftTextSize:
                        int textLeftSize = a.getDimensionPixelSize(attr, 0);
                        if (textLeftSize > 0) {
                            tvLeft.setTextSize(TypedValue.COMPLEX_UNIT_PX, textLeftSize);
                        }
                        break;
                    case R.styleable.TitleBar_tvTitleText:
                        String textTitle = a.getString(attr);
                        if (!TextUtils.isEmpty(textTitle)) {
                            tvTitle.setText(textTitle);
                        }
                        break;
                    case R.styleable.TitleBar_tvTitleTextColor:
                        int textTitleColor = a.getColor(attr, 0);
                        if (textTitleColor != 0) {
                            tvTitle.setTextColor(textTitleColor);
                        }
                        break;
                    case R.styleable.TitleBar_tvTitleTextSize:
                        int textTitleSize = a.getDimensionPixelSize(attr, 0);
                        if (textTitleSize != 0) {
                            tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textTitleSize);
                        }
                        break;
                    case R.styleable.TitleBar_tvRightVisibility:
                        tvRight.setVisibility(a.getBoolean(attr, false) ? VISIBLE : GONE);
                        break;
                    case R.styleable.TitleBar_tvRightText:
                        String textRight = a.getString(attr);
                        if (!TextUtils.isEmpty(textRight)) {
                            tvRight.setText(textRight);
                        }
                        break;
                    case R.styleable.TitleBar_tvRightTextSize:
                        int textRightSize = a.getDimensionPixelSize(attr, 0);
                        if (textRightSize > 0) {
                            tvRight.setTextSize(TypedValue.COMPLEX_UNIT_PX, textRightSize);
                        }
                        break;
                    case R.styleable.TitleBar_tvRightTextColor:
                        int textRightColor = a.getColor(attr, 0);
                        if (textRightColor != 0) {
                            tvRight.setTextColor(textRightColor);
                        }
                        break;
                    case R.styleable.TitleBar_dividerVisibility:
                        divider.setVisibility(a.getBoolean(attr, false) ? VISIBLE : GONE);
                        break;
                    case R.styleable.TitleBar_dividerColor:
                        int dividerColor = a.getColor(attr, 0);
                        divider.setBackgroundColor(dividerColor);
                        break;
                    case R.styleable.TitleBar_rightViewIcon:
                        if (a.getDrawable(attr) != null)
                            setRightViewIcon(a.getDrawable(attr));
                        break;
                    case R.styleable.TitleBar_rightViewIcon1:
                        if (a.getDrawable(attr) != null)
                            setRightViewIcon1(a.getDrawable(attr));
                        break;
                    case R.styleable.TitleBar_leftViewIcon:
                        if (a.getDrawable(attr) != null)
                            setLeftViewIcon(a.getDrawable(attr));
                        break;
                }
            }
            a.recycle();
        }
    }

    public void setRightViewIcon(Drawable icon) {
        if (iv_right_pic != null) {
            iv_right_pic.setImageDrawable(icon);
            iv_right_pic.setVisibility(VISIBLE);
        }
    }

    public void setRightViewIcon1(Drawable icon) {
        if (iv_right_pic1 != null) {
            iv_right_pic1.setImageDrawable(icon);
            iv_right_pic1.setVisibility(VISIBLE);
        }
    }

    public void setLeftViewIcon(Drawable icon) {
        if (iv_left_pic != null) {
            iv_left_pic.setImageDrawable(icon);
            iv_left_pic.setVisibility(VISIBLE);
        }
    }

    //设置右侧按钮监听事件
    public void setRightViewOnClickListener(OnClickListener listener) {
        if (ll_right != null)
            ll_right.setOnClickListener(listener);
    }

    public void setRightViewListener(OnClickListener listener) {
        if (iv_right_pic != null)
            iv_right_pic.setOnClickListener(listener);
    }

    public void setRightView1Listener(OnClickListener listener) {
        if (iv_right_pic1 != null)
            iv_right_pic1.setOnClickListener(listener);
    }

    //设置右侧按钮监听事件
    public void setLeftViewOnClickListener(OnClickListener listener) {
        if (ll_left != null)
            ll_left.setOnClickListener(listener);
    }

    /**
     * 设置标题
     *
     * @param title 标题
     */
    public void setTitleText(CharSequence title) {
        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
        }
    }

    /**
     * 右侧按钮 bt_title_left 设置点击事件
     *
     * @param listener OnClickListener
     */
    public void setRightBtClickListener(OnClickListener listener) {
        if (tvRight != null) {
            tvRight.setOnClickListener(listener);
        }
    }

    /**
     * 显示titleBar分割线
     * @param color
     */
    public void showDivider(@ColorInt int color){
        if(divider!=null){
            divider.setVisibility(VISIBLE);
            divider.setBackgroundColor(color);
        }
    }

    /**
     * 显示titleBar分割线，默认为#ddd
     */
    public void showDivider(){
        if(divider!=null){
            divider.setVisibility(VISIBLE);
        }
    }
}
